package businessLayer.businessObjects;

import java.util.ArrayList;

import dataAccessLayer.abstractFactory.Factory;
import dataAccessLayer.abstractFactory.daos.DAOingredients;
import dataAccessLayer.abstractFactory.daos.DAOmeasureUnity;
import dataAccessLayer.visualObject.VOingredient;
import dataAccessLayer.visualObject.VOmeasureUnity;

public class IngredientsBusinessObject 
{
	private DAOingredients daoIngredients;
	private DAOmeasureUnity daoMeasureUnity;
	
	public IngredientsBusinessObject(Factory factory)
	{
		daoIngredients = factory.getDAOingredients();
		daoMeasureUnity = factory.getDAOmeasureUnity();
	}

	public Object[][] getIngredients() 
	{
		ArrayList<VOingredient> ingredients = null;
		
		ingredients = daoIngredients.listIngredients();
		
		Object[][] resul = new Object[ingredients.size()][2];
		String[] measureUnities = new String[ingredients.size()];
		
		VOmeasureUnity vOmeasureUnity = new VOmeasureUnity();
		
		for (int i = 0; i < measureUnities.length; i++) 
		{
			vOmeasureUnity.setCode(ingredients.get(i).getMeasureUnity());
			measureUnities[i] = (daoMeasureUnity.searchMeasureUnityByCode(vOmeasureUnity)).getName();
		}
		
		for (int i = 0; i < ingredients.size(); i++) 
		{
			for (int j = 0; j < 2; j++) 
			{
				switch (j)
				{
				case 0:
					resul[i][j] = ingredients.get(i).getName();
					break;
				case 1:
					resul[i][j] = measureUnities[i];
					break;
				}
			}
		}
		
		return resul;
	}

	public String[] getMeasureUnities() 
	{
		ArrayList<VOmeasureUnity> measureUnities = daoMeasureUnity.listMeasureUnities();
		
		String[] resul = new String[measureUnities.size()];
		
		for (int i = 0; i < resul.length; i++) 
		{
			resul[i] = measureUnities.get(i).getName();
		}
		
		return resul;
	}

	public boolean insertMeasureUnity(String newMeasureUnity) 
	{
		VOmeasureUnity vOmeasureUnity = new VOmeasureUnity();
		vOmeasureUnity.setName(newMeasureUnity);
		
		int code = daoMeasureUnity.insertMeasureUnity(vOmeasureUnity);
		
		if(code>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public boolean insertIngredient(String name, int measureUnity) 
	{
		VOingredient vOingredient = new VOingredient();
		vOingredient.setName(name);
		vOingredient.setMeasureUnity(measureUnity);
		
		int code = daoIngredients.insertIngredient(vOingredient);
		
		if(code>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
